package com.jwtproject.bootserver.security.handler;

import cn.hutool.http.HttpStatus;
import com.jwtproject.bootserver.util.UtilResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description：自定义权限不足处理类
 * @remark: 自定义权限不足 需要做的业务操作
 * @explain 当用户登录系统后访问资源时因权限不足则会进入到此类并执行相关业务
 **/
@Slf4j
@Component
public class SecurityAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        StringBuffer msg = new StringBuffer("请求：");
        msg.append(request.getRequestURI()).append(" 权限不足，无法访问系统资源.");
        log.info(msg.toString());
        UtilResponse.writeFailureResult(response, HttpStatus.HTTP_FORBIDDEN, msg.toString());
    }
}
